#include <iostream>
#include <stdlib.h>
#include <string.h>
#include "fusecpp.h"
#include "fileman.h"
#include "mvfs.h"
#include "config.h"
#include "ossstor.h"
#include "gcstor.h"
#include "kp.h"

using namespace std;

static fuse_cpp::FuseDispatcher fuseDis;

FileManager *pMan;
OnlineStorage *oss, *gc, *kp;

void storageTest(OnlineStorage *os) {
    int res;
    char *filec = "This is the file content";
    int len = strlen(filec);
    char buf[1000];
    NetFile *f;
    int size;

    memset(buf,0,1000);

    res = os -> connect();
    if (res == 0) {
        cout << "Connected" << endl;
    } else {
        cout << "error:Not Connected" << endl;
        goto cleanbuf;
    }

    res = os -> put("/testfile", filec, len);

    if (res >= 0) {
        cout << "Put done" << endl;
    } else {
        cout << "error:Unable to put" << endl;
        //goto cleanbuf;
    }

    res = os -> get("/testfile", buf, len);
    if (res >= 0) {
        cout << "Get done" << endl;
    } else {
        cout << "error:Unable to get" << endl;
        goto cleanbuf;
    }
    buf[len] ='\0';
    if (strcmp(filec, buf) != 0) {
        cout << "error:File not consistent" << endl;
        goto cleanbuf;
    }


    res = os -> mkdir("/", "testdir");
    if (res == 0) {
        cout << "mkdir done" << endl;
    } else {
        cout << "error:Unable to mkdir" << endl;
        //goto cleanbuf;
    }

    //res = os -> mkdir("/", "e");

    f = os -> lsdir("/e/", &size);
    if (f != NULL) {
        cout << "files are:" << endl;
        for (int i = 0; i < size; i++) {
            cout << f[i].fileName << " , " << f[i].folder << " , " << f[i].size << endl;
        }
    } else {
        cout << "error:no files" << endl;
        goto cleanbuf;
    }

    res = os -> rmdir("/", "testdir");
    if (res == 0) {
        cout << "rmdir done" << endl;
    } else {
        cout << "error: cannot rm dir" << endl;
        goto cleanbuf;
    }

    res = os -> move("/testfile", "/testfile2");
    if (res == 0) {
        cout << "move done" << endl;
    } else {
        cout << "error: cannot move" << endl;
        goto cleanbuf;
    }

    res = os -> del("/testfile2");
    if (res == 0) {
        cout << "del done" << endl;
    } else {
        cout << "error cannot del" << endl;
        goto cleanbuf;
    }

    f = os -> lsdir("/", &size);
    if (f != NULL) {
        cout << "files are:" << endl;
        for (int i = 0; i < size; i++) {
            cout << f[i].fileName << " , " << f[i].folder << " , " << f[i].size << endl;
        }
    } else {
        cout << "no files" << endl;
        goto cleanbuf;
    }

    os -> disconnect();
cleanbuf:
    //free(buf);
    return;
}

int main(int argc, char* argv[])
{
    Config::Load("moss.cfg");
    pMan = new FileManager();
    char c;

    GCStorage *gce = new GCStorage();

    oss = new OSSStorage();
    gc = gce;
    kp = new KPOnlineStorage();

    cout << "starting test oss......(y/n)";
    cin >> c;
    if (c == 'y') {
        storageTest(oss);
    }
    cout << "oss test done." << endl;

    cout << "starting test gc......(y/n)";
    cin >> c;
    if (c == 'y') {
        gce -> init();
        storageTest(gc);
        gce -> destroy();
    }
    cout << "gc test done." << endl;

    cout << "starting test kp......(y/n)";
	string s;
    cin >> s;

     
    if (s == "y") {
        storageTest(kp);
    }
    cout << "kp test done." << endl;


    delete oss;
    delete gc;
    delete kp;
    delete pMan;
    return 0;
}
